rm(list=ls())

library(tidyr)
library(tibble)
library(stargazer)
library(dplyr)
library(haven)
library(readstata13)
library(ggplot2)
library(zoo)
library(ggridges)
library(ggpubr)




#-------------------------------------#
# REPLICATION MANUSCRIPT FIGURES      #
# CPS MAYORS                          #
#                                     #
# JESSICA ZARKIN NOTNI                #
# JUNE 2024.                          #
#-------------------------------------#

#-------------------- 1. LOAD DATA ####

colo <- read.dta13("6_Replication/1_Data/ColombiaFig4_5.dta")
colo100plus <- subset(colo, city100==1)

colo_all <- read.dta13("6_Replication/1_Data/ColombiaFig6.dta")
bogota <- subset(colo_all, municipio=="Bogotá, D.C.")

bra <- read.dta13("6_Replication/1_Data/SPFig8.dta")



#-------------------- 2. FIGURE 4.   ####


hom <- ggplot(colo100plus, aes(x=rate_homicidios, y=comp_fonsetpcdol, label=namesh_mpio)) + 
  geom_point(alpha = 0.8) +
  geom_text(size = 1.5,vjust = -1.5) +
  ylab("Average FONSET expenditures, 13-20 (USD pc)") + 
  xlab("Average homicide rate, 13-19") +  
  geom_smooth(method=lm, colour="black") + 
  theme(legend.position = "bottom",
        panel.background = element_rect(fill = "#f7f7f7",colour = "grey50"),
        axis.text.x = element_text(size = 8),
        axis.text.y = element_text(size = 8),
        axis.title.x = element_text(size =10),
        axis.title.y = element_text(size =10)) 


plot(hom)

ggsave("4_Figures/CPS/Figure 4.png", plot=hom, width=7,height=5, units = "in", dpi = 500)


#-------------------- 3. FIGURE 5    ####


spent <- ggplot(colo100plus, aes(x=y_corr_tributpcdol, y=comp_fonsetpcdol, label=namesh_mpio)) + 
  geom_point(alpha = 0.8) +
  geom_text(size = 1.5,vjust = -1.5) +
  geom_smooth(method=lm, colour="black") + 
  ylab("Average FONSET expenditure, 13-20 (USD pc)") + 
  xlab("Average tax collection, 13-20 (USD pc)") +  
  theme(legend.position = "bottom",
        panel.background = element_rect(fill = "#f7f7f7",colour = "grey50"),
        axis.text.x = element_text(size = 8),
        axis.text.y = element_text(size = 8),
        axis.title.x = element_text(size =10),
        axis.title.y = element_text(size =10)) 


plot(spent)

unspent <- ggplot(colo100plus, aes(x=y_corr_tributpcdol, y=negpcejecfonset, label=namesh_mpio)) + 
  geom_point(alpha = 0.8) +
  geom_text(size = 1.5,vjust = -1.5) +
  geom_smooth(method=lm, colour="black") + 
  ylab("Average % FONSET budget unspent (13-20)") + 
  xlab("Average tax collection, 13-20 (USD pc)") +  
  theme(legend.position = "bottom",
        panel.background = element_rect(fill = "#f7f7f7",colour = "grey50"),
        axis.text.x = element_text(size = 8),
        axis.text.y = element_text(size = 8),
        axis.title.x = element_text(size =10),
        axis.title.y = element_text(size =10)) 


plot(unspent)

fonset<-ggarrange(spent, unspent, ncol=2, nrow=1)

plot(fonset)

ggsave("4_Figures/CPS/Figure 5.png", plot=fonset, width=9,height=5, units = "in", dpi = 500)

#-------------------- 5. FIGURE 6.     ####


gp <- data.frame(x=2014, y=180, label="Gustavo Petro")
ep <- data.frame(x=2018, y=180, label="Enrique Peñalosa")
cl <- data.frame(x=2020, y=180, label="Claudia López")

bogfons <- ggplot(data=bogota, aes(x=year, y=comp_fonsetpcdol)) +
  geom_line() + 
  scale_x_continuous(name="Year", limits=c(2013, 2021), breaks=seq(2000,2021,1)) + 
  scale_y_continuous(name="FONSET expenditures (USD pc)", limits=c(0, 180)) +
  geom_vline(xintercept=2011, linetype="dashed", color = "#636363") + 
  geom_vline(xintercept=2015, linetype="dashed", color = "#636363") + 
  geom_vline(xintercept=2019, linetype="dashed", color = "#636363") + 
  geom_label(aes(x = x,y=y,label=label),size=2, fill="white", color="black",data=gp) + 
  geom_label(aes(x = x,y=y,label=label),size=2, fill="white", color="black",data=ep) + 
  geom_label(aes(x = x,y=y,label=label),size=2, fill="white", color="black",data=cl) 

plot(bogfons)


boghom <- ggplot(data=bogota, aes(x=year, y=rate_homicidios)) +
  geom_line() + 
  scale_x_continuous(name="Year", limits=c(2000, 2020), breaks=seq(2000,2020,2)) + 
  scale_y_continuous(name="Homicide rate", limits=c(5,30))

plot(boghom)

bogfig<-ggarrange(bogfons, boghom, ncol=2, nrow=1)

plot(bogfig)

ggsave("4_Figures/CPS/Figure 6.png", plot=bogfig, width=11,height=5, units = "in", dpi = 500)


#-------------------- 5. FIGURE 8.     ####

bra$parallel<-as.factor(bra$parallel)

ridge_all <- ggplot(bra, aes(x = ingresopresp2017pc, y = parallel, group = parallel)) + 
  stat_density_ridges(quantile_lines = TRUE, quantiles = 0.5,aes(fill = parallel), scale = 1.3, alpha = 0.7) +
  theme(legend.position = "none") + 
  scale_fill_manual(values = c("#d9d9d9", "#969696", "#525252", "#000000")) + 
  xlab("Property tax revenue pc") + ylab("Linkage") + 
  scale_y_discrete(breaks=c("0","1","2", "3"),
                   labels=c("Accomodation", "Parallel: AD", "Parallel: GM", "Parallel: Both"))

plot(ridge_all)

ggsave("4_Figures/CPS/Figure 8.png", plot=ridge_all, width=7,height=5, units = "in", dpi = 300)

